const BASE_URL = 'http://localhost:9527'

// 微服务前缀映射
const SERVICE_PREFIX = {
  LOGIN: '/Login',   // 用户登录微服务
  ARTICLE: '/Article',  // 文章微服务
  VIDEO: '/Video',  // 视频微服务
  DEFAULT: ''  // 默认无前缀
}

// 封装请求方法，支持指定微服务
const request = (options) => {
  // 确定服务前缀
  const servicePrefix = options.servicePrefix || SERVICE_PREFIX.DEFAULT
  
  return new Promise((resolve, reject) => {
    const token = uni.getStorageSync('token')
    
    uni.request({
      url: BASE_URL + servicePrefix + options.url,
      method: options.method || 'GET',
      data: options.data || {},
      header: {
        'content-type': 'application/json',
        'Authorization': token ? `Bearer ${token}` : '',
        ...options.header
      },
      success: (res) => {
        // 请求成功，检查状态码
        if (res.statusCode === 200) {
          // 业务逻辑成功
          if (res.data.code === 0 || res.data.code === 200) {
            resolve(res.data)
          } else if (res.data.code === 401) {
            // token失效，需要重新登录
            uni.removeStorageSync('token')
            uni.showToast({
              title: '登录已过期，请重新登录',
              icon: 'none'
            })
            setTimeout(() => {
              uni.navigateTo({
                url: '/pages/login/index'
              })
            }, 1500)
            reject(res.data)
          } else {
            // 业务逻辑失败
            uni.showToast({
              title: res.data.message || '请求失败',
              icon: 'none'
            })
            reject(res.data)
          }
        } else {
          // HTTP请求失败
          uni.showToast({
            title: '网络请求失败',
            icon: 'none'
          })
          reject(res)
        }
      },
      fail: (err) => {
        uni.showToast({
          title: '网络异常，请检查网络设置',
          icon: 'none'
        })
        reject(err)
      }
    })
  })
}

// 导出请求方法
export default {
  // GET请求
  get(url, data = {}, options = {}) {
    return request({
      url,
      data,
      method: 'GET',
      ...options
    })
  },
  
  // POST请求
  post(url, data = {}, options = {}) {
    return request({
      url,
      data,
      method: 'POST',
      ...options
    })
  },
  
  // PUT请求
  put(url, data = {}, options = {}) {
    return request({
      url,
      data,
      method: 'PUT',
      ...options
    })
  },
  
  // DELETE请求
  delete(url, data = {}, options = {}) {
    return request({
      url,
      data,
      method: 'DELETE',
      ...options
    })
  },
  
  // 针对不同微服务的请求方法
  login: {
    get(url, data = {}, options = {}) {
      return request({
        url,
        data,
        method: 'GET',
        servicePrefix: SERVICE_PREFIX.LOGIN,
        ...options
      })
    },
    post(url, data = {}, options = {}) {
      return request({
        url,
        data,
        method: 'POST',
        servicePrefix: SERVICE_PREFIX.LOGIN,
        ...options
      })
    }
  },
  
  article: {
    get(url, data = {}, options = {}) {
      return request({
        url,
        data,
        method: 'GET',
        servicePrefix: SERVICE_PREFIX.ARTICLE,
        ...options
      })
    },
    post(url, data = {}, options = {}) {
      return request({
        url,
        data,
        method: 'POST',
        servicePrefix: SERVICE_PREFIX.ARTICLE,
        ...options
      })
    }
  }
}

// 导出常量
export { BASE_URL, SERVICE_PREFIX } 